# coding:utf-8
import MySQLdb
def createTop10Panel(topclass, name, listData):
  res = "<li><div style=''><h3><a href='stockpanellist.do?topname=%s'><b>%s</b></a></h3><table width='100%%' cellspacing='0' cellpadding='0' border='0'>" % (topclass, name);
  res += "<tr class='head'><th>代码</th><th>名称</th><th>当前价</th><th>涨跌</th></tr>" 
  for i in listData:
    flag="green"
    if i[3][0]=='+':
      flag="red"
    tmp = "<tr><td><a href='http://stockdata.stock.hexun.com/%s.shtml'>%s</a></td><td>%s</td><td>%s</td><td class='%s'>%s</td></tr>" % (i[0], i[0], i[1], i[2], flag, i[3])
    res += tmp
  res += "</table></div></li>"
  return res

def loadTopPanels():
  conn=MySQLdb.connect(host="10.241.84.65",user="root",db="stock")
  cursor = conn.cursor()
  sql = "select stock_class.des,stock_class.code,name,stock_info.curprice,stock_info.curwave,stock_class.class from stock_class,stock_info where stock_class.code=stock_info.code order by abs(stock_info.curwave) desc";
  cursor.execute(sql)
  alldata = cursor.fetchall()
  tops=dict()
  names=dict()
  for i in alldata:
    #print i[2]
    if not i[0] or not i[1] or not i[2] or not i[3] or not i[4]:
      continue
    pstr=""
    if i[4] < 0:
      #print i
      pstr = "%.2f%%" % (i[4])
    else:
      pstr = "+%.2f%%" % (i[4])
    wstr = "%.2f" % (i[3])
    if i[5] not in tops:
      tops[i[5]] = list()
      names[i[5]] = i[0]
    if len(tops[i[5]]) < 10:
      tops[i[5]].append([i[1],i[2],wstr,pstr])
  res=""
  for i in tops:
    res+=createTop10Panel(i, names[i], tops[i])
  res = "<div class='stockview' style='margin-top:20px'><ul class='stocklist'>%s</ul></div>" % (res)
  return res
